//
//  APBottomMenuViewController.m
//  AqualPolo
//
//  Created by dong on 2024/5/6.
//

#import "APBottomMenuViewController.h"
#import "APBottomMenuView.h"
#import "APBroadcasterModel.h"
@interface APBottomMenuViewController ()<APBottomMenuViewDelegate,APBottomReportMenuViewDelegate>
@property (nonatomic, strong) APBottomMenuView *menuView;
@property (nonatomic, strong) APBottomReportMenuView *reportMenuView;
@property (nonatomic, strong) APBroadcasterModel *userModel;
@end

@implementation APBottomMenuViewController
- (APBottomMenuView *)menuView{
    if (!_menuView) {
        _menuView = [[APBottomMenuView alloc] init];
        _menuView.delegate = self;
        [_menuView.cancelButton addTarget:self action:@selector(cancelButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _menuView;
}

- (APBottomReportMenuView *)reportMenuView{
    if (!_reportMenuView) {
        _reportMenuView = [[APBottomReportMenuView alloc] init];
        _reportMenuView.delegate = self;
        [_reportMenuView.cancelButton addTarget:self action:@selector(cancelButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _reportMenuView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = LYRGBAColor(0, 0, 0, 0.6);
    [self.view addSubview:self.menuView];
    [self.menuView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
    }];
    
    self.userModel = [[APUserDataManager shareInstance] searchUserByUserId:_userId];
    
}

- (void)setUserModel:(APBroadcasterModel *)userModel{
    _userModel = userModel;
    self.menuView.followItem.titleLabel.text = userModel.isFriend ? @"Unfollow":@"Follow";
}


- (void)menuViewDidClickItem:(NSInteger)index{
    if (index == 2) {
        [self.menuView removeFromSuperview];
        [self.view addSubview:self.reportMenuView];
        [self.reportMenuView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
        }];
    }else{
        if (index == 0) {
            [self followDidClick];
        }else{
            [self.view ShowProgress];
            [[APUserDataManager shareInstance] blockUser:_userId result:^(_Bool success, NSString * _Nonnull msg) {
                [self.view removeProgress];
                if (success) {
                    [self.view ShowProgressOnlyLabel:msg];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [self dismissViewControllerAnimated:true completion:nil];
                        if (self.callBack) {
                            self.callBack(1);
                        }
                    });
                }else{
                    [self dismissViewControllerAnimated:true completion:nil];
                }

            }];
            [APLogManager statisticWithPage:APPage_block];
        }
    }
}

- (void)followDidClick{
    [self.view ShowProgress];
    WS(ws);
    if (_userModel.isFriend) {
        [[APUserDataManager shareInstance] unfollow:_userId result:^(_Bool success, NSString * _Nonnull msg) {
            if (success) {
                [self.view ShowProgressOnlyLabel:msg];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    if (ws.callBack) {
                        ws.callBack(0);
                    }
                    [self dismissViewControllerAnimated:true completion:nil];
                });
            }else{
                [self dismissViewControllerAnimated:true completion:nil];
            }
        }];
    } else {

        [[APUserDataManager shareInstance] follow:_userId result:^(_Bool success, NSString * _Nonnull msg) {
            [self.view removeProgress];
            if (success) {
                [self.view ShowProgressOnlyLabel:msg];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    if (ws.callBack) {
                        ws.callBack(0);
                    }
                    [self dismissViewControllerAnimated:true completion:nil];
                });
            }else{
                [self dismissViewControllerAnimated:true completion:nil];
            }
        }];
    }

}

- (void)reportMenuViewDidClickItem:(NSInteger)index{
    NSArray *titles = @[
      @"Pornographic",
      @"False gender",
      @"Fraud",
      @"Political sensitive",
      @"Other"
    ];
    
    NSDictionary *parameters = @{
       @"broadcasterId": self.userId,
       @"complainSub": titles[index],
       @"complainCategory": @"Report"
     };
    [self.view ShowProgress];
    [APDataManager postJsonWithUrlString:insertBlockRecord timeoutInterval:0 parameters:parameters result:^(NSDictionary * _Nullable res) {
        [self.view ShowProgressOnlyLabel:@"Report Successfully"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:true completion:nil];
        });
    } failBlock:^(NSString * _Nullable errorMsg) {
        [self.view removeProgress];
        [self dismissViewControllerAnimated:true completion:nil];
    }];
    [APLogManager statisticWithPage:APPage_report];

    
}


- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self dismissViewControllerAnimated:true completion:nil];
}

- (void)cancelButtonDidClick:(UIButton *)sender{
    [self dismissViewControllerAnimated:true completion:nil];

}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    }
    return self;
}

@end


